package com.joysuch.wwyt.edu.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.bp.entity.BpJob;
import com.joysuch.wwyt.bp.repository.BpDepartDao;
import com.joysuch.wwyt.bp.repository.BpJobDao;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.edu.bean.EduExamDto;
import com.joysuch.wwyt.edu.entity.EduExam;
import com.joysuch.wwyt.edu.entity.EduExamUserScope;
import com.joysuch.wwyt.edu.entity.EduTestPaper;
import com.joysuch.wwyt.edu.mapper.EduExamMapper;
import com.joysuch.wwyt.edu.mapper.EduExamUserScopeMapper;
import com.joysuch.wwyt.edu.mapper.EduTestPaperMapper;
import com.joysuch.wwyt.edu.repository.EduExamDao;
import com.joysuch.wwyt.edu.repository.EduExamUserScopeDao;
import com.joysuch.wwyt.edu.repository.EduTestPaperDao;
import com.joysuch.wwyt.edu.service.EduTestPaperService;
import groovy.util.logging.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

@Slf4j
@Component
public class ExamPlanTask {

    @Autowired
    private EduExamMapper eduExamMapper;

    @Autowired
    private EduExamUserScopeMapper eduExamUserScopeMapper;

    @Autowired
    private EduExamDao ExamDao;

    @Autowired
    private EduExamUserScopeDao ScopeDao;

    @Autowired
    private BaseUserDao baseUserDao;

    @Autowired
    private BpDepartDao bpDepartDao;

    @Autowired
    private BpJobDao jobDao;

    @Autowired
    private EduTestPaperMapper eduTestPaperMapper;

    @Autowired
    private EduTestPaperService eduTestPaperService;

    @Scheduled(cron = "0 0/35 * * * ? ")
    public void initializedData() {
        EduExam exam = eduExamMapper.selectByFromType();
        if (exam == null) {
            List<EduExam> exams = eduExamMapper.selectExam();
            List<EduExam> eduExams = eduExamMapper.selectByRecord();
            if (!eduExams.isEmpty()) {
                eduExamMapper.updateByExamFrom(eduExams);
                ;
            }
            if (!exams.isEmpty()) {
                List<EduExam> eduExamList = exams.stream().filter(edu -> eduExams.stream().noneMatch(e -> e.getId().equals(edu.getId()))).collect(Collectors.toList());
                if (!eduExamList.isEmpty()) {
                    eduExamMapper.updateByTrainType(eduExamList);
                }
            }
            EduExam eduExam = new EduExam();
            eduExam.setDeleteFlag("1");
            eduExam.setFormType(3);
            eduExam.setTrainType(3);
            eduExamMapper.insert(eduExam);

        }
    }

    @Scheduled(cron = "0 0/35 * * * ? ")
    public void scopeUpdate() {
        EduExamUserScope userScope = eduExamUserScopeMapper.selectByObjType();
        if (userScope == null) {
            List<EduExamUserScope> eduExamUserScopes = eduExamUserScopeMapper.scopeUpdate();
            List<EduExamUserScope> examUserScopes = new ArrayList<>();
            if (!eduExamUserScopes.isEmpty()) {
                for (EduExamUserScope eduExamUserScope : eduExamUserScopes) {
                    List<BaseUser> baseUsers = new ArrayList<>();
                    if (eduExamUserScope.getObjId() != null && eduExamUserScope.getObjType().equals("J")) {
                        BpJob one = jobDao.getOne(eduExamUserScope.getObjId());
                        baseUsers = baseUserDao.getJpbId(one.getId());
                    }
                    if (eduExamUserScope.getObjId() != null && eduExamUserScope.getObjType().equals("D")) {
                        BpDepart one = bpDepartDao.getOne(eduExamUserScope.getObjId());
                        baseUsers = baseUserDao.getPartId(one.getId());
                    }
                    for (BaseUser baseUser : baseUsers) {
                        EduExamUserScope examUserScope = new EduExamUserScope();
                        BeanUtils.copyProperties(eduExamUserScope, examUserScope);
                        examUserScope.setObjId(baseUser.getId());
                        examUserScope.setObjType("U");
                        examUserScope.setId(null);
                        examUserScopes.add(examUserScope);
                    }
                    eduExamUserScope.setDeleteFlag("1");
                    ScopeDao.save(eduExamUserScope);
                }
                ArrayList<EduExamUserScope> collect = examUserScopes.stream().collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(
                                        () -> new TreeSet<>(
                                                Comparator.comparing(
                                                        o -> o.getExamId() + ";" + o.getObjId()))), ArrayList::new));
                eduExamUserScopeMapper.saveAll(collect);
            }
            EduExamUserScope scope = new EduExamUserScope();
            scope.setDeleteFlag("1");
            scope.setObjType("K");
            eduExamUserScopeMapper.insert(scope);
        }
    }

    /**
     * 试卷是否阅卷处理老数据 只运行一次
     */
//    @Scheduled(cron = "0/10 * * * * ?")
    @Scheduled(cron = "0 0/35 * * * ? ")
    public void checkTestPaper() {
        if (eduTestPaperMapper.selectSole()>0){
            return;
        }
        List<EduTestPaper> eduTestPapers = eduTestPaperMapper.selectList(new LambdaQueryWrapper<EduTestPaper>().eq(EduTestPaper::getDeleteFlag, "0"));
        eduTestPapers.forEach(eduTestPaper -> {
            if(eduTestPaperService.checkTestPaperContainZgt(eduTestPaper)){
                eduTestPaper.setIsSupervisorQuestion(1);
            }else {
                eduTestPaper.setIsSupervisorQuestion(0);
            }
            eduTestPaperMapper.updateById(eduTestPaper);
        });
        EduTestPaper eduTestPaper = new EduTestPaper();
        eduTestPaper.setCode("WY01");
        eduTestPaper.setDeleteFlag("1");
        eduTestPaperMapper.insert(eduTestPaper);
    }

}
